package com.bignerdranch.android.ecar.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private ImageButton close;
    private TextView forget;
    private TextView new_register;
    private Button register;
    private RelativeLayout container;
    private EditText phone;
    private EditText pass;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        close = (ImageButton) findViewById(R.id.close);
        forget = (TextView) findViewById(R.id.forget);
        forget.setOnClickListener(this);
        new_register = (TextView) findViewById(R.id.new_register);
        new_register.setOnClickListener(this);
        register = (Button) findViewById(R.id.register);
        container = (RelativeLayout) findViewById(R.id.container);
        container.setOnClickListener(this);
        close.setOnClickListener(this);
        register.setOnClickListener(this);

        phone = (EditText) findViewById(R.id.phone);
        phone.setOnClickListener(this);
        pass = (EditText) findViewById(R.id.pass);
        pass.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.register:
                String url =URLBank.teacher_url+URLBank.login_url+"?phone="+phone.getText().toString()+"&password="+pass.getText().toString();
                Log.e("======",url+"");
                RequestUtil.executeJSONObject(this,url , new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject o) {
                        try {
                            String code = o.getString("code");
//                            Log.e("===code===",o.getString("code"));
                            if (code.equals("0")){
                                String userName = o.getJSONObject("data").getString("userName");
                                String phone = o.getJSONObject("data").getString("phone");
                                String password = o.getJSONObject("data").getString("password");
                                String headPic = o.getJSONObject("data").getString("headPic");
                                SharedPreferences sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putBoolean("login_status",true);
                                editor.putString("userName",userName);
                                editor.putString("phone",phone);
                                editor.putString("password",password);
                                editor.putString("headPic",headPic);
                                editor.commit();
                                Intent intent = new Intent();
                                intent.putExtra("userName",userName);
                                intent.putExtra("login_status",true);
                                intent.putExtra("headPic",headPic);
                                setResult(RESULT_OK,intent);
                                finish();
                            }
                            Log.e("======",code);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });

//                finish();
                break;
            case R.id.forget:
                Intent intent1 = new Intent(this,Retrieve_01Activity.class);
                startActivity(intent1);
                break;
            case R.id.new_register:
                Intent intent = new Intent(this,SignActivity.class);
                startActivity(intent);
                break;
            case R.id.container:
                InputMethodManager imm = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                break;
        }
    }

}
